/**********************************************************************************************************************
 * \file Cpu0_Main.c
 * \copyright Copyright (C) Infineon Technologies AG 2019
 * 
 * Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of 
 * business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
 * are agreed, use of this file is subject to following:
 * 
 * Boost Software License - Version 1.0 - August 17th, 2003
 * 
 * Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and 
 * accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
 * and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
 * Software is furnished to do so, all subject to the following:
 * 
 * The copyright notices in the Software and this entire statement, including the above license grant, this restriction
 * and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all 
 * derivative works of the Software, unless such copies or derivative works are solely in the form of 
 * machine-executable object code generated by a source language processor.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE 
 * COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE.
 *********************************************************************************************************************/
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "aurix_pin_mappings.h"
#include "Bsp.h"       /*for the default timer*/

IfxCpu_syncEvent g_cpuSyncEvent = 0;
void SPKGPIOToogle(void){
    if(IfxPort_getPinState(DIAL_0.port,DIAL_0.pinIndex) == 0){
        IfxPort_togglePin(SPEAKER.port,SPEAKER.pinIndex);
    }
    else{
        IfxPort_setPinLow(SPEAKER.port,SPEAKER.pinIndex);
    }
}
void SPKGPIO(void){
    if(IfxPort_getPinState(DIAL_0.port,DIAL_0.pinIndex) == 0){
        IfxPort_setPinHigh(SPEAKER.port,SPEAKER.pinIndex);
    }
    else{
        IfxPort_setPinLow(SPEAKER.port,SPEAKER.pinIndex);
    }
}
int CycleSpeed(int timer){
    if(timer == 500){
        return 2000;
    }
    else{
       return timer-500;
    }
}
int SparkLED(int mode,int time){
    IfxPort_togglePin(LED_0.port,LED_0.pinIndex);
    SPKGPIOToogle();
    if(!mode){
        waitTime(IfxStm_getTicksFromMilliseconds(BSP_DEFAULT_TIMER,time));
        }
    return 0;
}
void core0_main(void)
{
    IfxCpu_enableInterrupts();
    
    /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdogs and service them periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
    IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
    
    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);

    /*define the param of the state*/
    int state_key0 = 1;
    int state_key1 = 1;
    int mode = 1;
    int time = 2000;
    /*initialization*/
    gpio_init_pins();
    /*set default pin state */
    IfxPort_setPinHigh(LED_0.port,LED_0.pinIndex);
    IfxPort_setPinHigh(LED_1.port,LED_1.pinIndex);
    IfxPort_setPinLow(SPEAKER.port,SPEAKER.pinIndex);
//      Led0-> show;Led1-> show manual mode
//      KO-> shift const/spark mode;K1-> shift the speed;K2-> manual control
//      dial0-> control the speaker opening; dial1-> control the manual opening



    /*tick event*/
    while(1)
    {
        /*shift mode*/
        if(!IfxPort_getPinState(KEY_0.port,KEY_0.pinIndex)){
            if(state_key0==1){
                mode = !mode;
                state_key0 = 0;
            }
        }
        else{
            state_key0 = 1;
        }


        /*shift auto speed*/
        if(!IfxPort_getPinState(KEY_1.port,KEY_1.pinIndex)){
                    if(state_key1==1){
                        time = CycleSpeed(time);
                        state_key1 = 0;
                    }
                }
        else{
            state_key1 = 1;
        }


        /*manual mode*/
        if(!IfxPort_getPinState(DIAL_1.port,DIAL_1.pinIndex)){
            IfxPort_setPinLow(LED_1.port,LED_1.pinIndex);
            if(!IfxPort_getPinState(KEY_2.port,KEY_2.pinIndex)){
                IfxPort_setPinLow(LED_0.port,LED_0.pinIndex);
                SPKGPIO();
            }
            else{
                IfxPort_setPinHigh(LED_0.port,LED_0.pinIndex);
                IfxPort_setPinLow(SPEAKER.port,SPEAKER.pinIndex);
            }
        }
        /*auto mode*/
        else{
            /*intialize*/
            IfxPort_setPinHigh(LED_1.port,LED_1.pinIndex);
            SparkLED(mode,time);
        }
    }
}
